package com.airsystem.pos.bengkel.service.converter;

import java.math.BigDecimal;

import javax.swing.JTable;

import com.airsystem.pos.bengkel.entity.DetailKendaraan;
import com.airsystem.pos.bengkel.entity.DetailPesanSparepart;
import com.airsystem.pos.bengkel.entity.DetailSparepart;
import com.airsystem.pos.bengkel.entity.JenisSparepart;
import com.airsystem.pos.bengkel.entity.Kendaraan;
import com.airsystem.pos.bengkel.entity.PesanSparepart;
import com.airsystem.pos.bengkel.entity.Sparepart;
import com.airsystem.pos.bengkel.interfaces.IParser;
import com.airsystem.pos.bengkel.model.SparepartOrderModel;

/**
 * @author Fanny Irawan Sutawanir (fannyirawans@gmail.com)
 */
public class SparepartTableConverter implements IParser<PesanSparepart, JTable> {
	private SparepartOrderModel sparepartOrderModel;

	public SparepartTableConverter() {
		sparepartOrderModel = new SparepartOrderModel();
	}

	public PesanSparepart doParse(JTable data) {
		PesanSparepart pesanSparepart = new PesanSparepart();
		pesanSparepart.setTanggal(sparepartOrderModel.getTanggalChooser().getDate());

		for (int i = 0; i < sparepartOrderModel.getTableSpareparts().getRowCount(); i++) {
			String sparepart = sparepartOrderModel.getTableSpareparts().getValueAt(i, 0).toString();
			String[] sparepartItems = sparepart.split("-");
			String kendaraan = sparepartOrderModel.getTableSpareparts().getValueAt(i, 1).toString();
			String[] kendaraanItems = kendaraan.split("-");
			String harga = sparepartOrderModel.getTableSpareparts().getValueAt(i, 3).toString();
			String pesan = sparepartOrderModel.getTableSpareparts().getValueAt(i, 4).toString();
			String distributor = sparepartOrderModel.getTableSpareparts().getValueAt(i, 6).toString();
			String metode = sparepartOrderModel.getTableSpareparts().getValueAt(i, 7).toString();
			String jenisSparepartID = sparepartOrderModel.getTableSpareparts().getValueAt(i, 8).toString();

			Sparepart merkSparepart = new Sparepart();
			merkSparepart.setMerk(sparepartItems[0].trim());

			JenisSparepart jenisSparepart = new JenisSparepart();
			jenisSparepart.setId(Long.parseLong(jenisSparepartID));

			Kendaraan merkKendaraan = new Kendaraan();
			merkKendaraan.setMerk(kendaraanItems[0].trim());

			DetailKendaraan submerkKendaraan = new DetailKendaraan();
			submerkKendaraan.setSub_merk(kendaraanItems[1].trim());
			submerkKendaraan.setKendaraan(merkKendaraan);

			DetailSparepart detailSparepart = new DetailSparepart();
			detailSparepart.setSparepart(merkSparepart);
			detailSparepart.setJenis_sparepart(jenisSparepart);
			detailSparepart.setSub_merk(sparepartItems[1].trim());
			detailSparepart.setDetail_kendaraan(submerkKendaraan);

			DetailPesanSparepart detailPesanSparepart = new DetailPesanSparepart();
			detailPesanSparepart.setMetode(metode);
			detailPesanSparepart.setDistributor(distributor);
			detailPesanSparepart.setHarga(new BigDecimal(harga));
			detailPesanSparepart.setStok(Long.parseLong(pesan));
			detailPesanSparepart.setDetail_sparepart(detailSparepart);
			detailPesanSparepart.setPesan_sparepart(pesanSparepart);

			pesanSparepart.addDetail_pesan_sparepart(detailPesanSparepart);
		}

		return pesanSparepart;
	}
}